import channals from './data/channals' 
import articles from './data/article' 

const Mock = require('mockjs')

const getResponse=(url,type,data,condition)=>{
    return{
        url,
        type:type||'get',
        response:(config,query)=>{
            console.log(url,config,type,data);
            // let copyData = []
            // data.article.filter(item=>{
            //     let thData = item
            //     if(query.get('status')!=='all' && query.get('status')===item.status ){
            //         if(query.get('channel_id')!==''){
                        
            //         }
            //     }
            // })

            return condition?{
                code:200,
                success:true,
                message:'请求成功',
                content:{
                    data
                }
            }:{
                code: 500,
                success: false,
                message: "请求数据失败",
              };
        }
    }
}

const mocks = [
    getResponse('/channel','get',channals,true),
    getResponse('/mp/articles','get',articles,true)
]

export const mockXHR = () => {
    for (const i of mocks) {
        
        Mock.mock(new RegExp(i.url + '(\\?.*)?'), i.type || "get", (config)=>{
            console.log(config,'config---')
            // 解析查询参数
            const query = config.url.split('?')[1] || '';
            console.log(query,'query');
            const params = new URLSearchParams(query);
            return i.response(config, params);
        });
    }
};

// const getChannal = Mock.mock(/channel/,'get',()=>{
//     return {
//         code:200,
//         msg:'success',
//         data:channals
//     }
// })
// const getArticles = Mock.mock(/mp\/articles/,'get',()=>{
//     return {
//         code:200,
//         msg:'success',
//         data:articles
//     }
// })